﻿using System.Text.RegularExpressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System;

namespace ReadyApps.Models.Wiki
{
    class WikiPageLink : IWikiPageComponent
    {
        #region Variables

        public static readonly Regex MatchComparison = new Regex(@"\{\[link:(\d+)\]\}", RegexOptions.Compiled);

        #endregion

        #region Properties

        public int PageLinkKey { get; private set; }
        public string DisplayText { get; private set; }
        public string LinkUrl { get; private set; }
        public string Target { get; private set; }

        #endregion

        #region Constructor

        /// <summary>
        /// Creates a new instance of a WikiPageLink with the specified page link key, display text,
        /// link URL, and target.
        /// </summary>
        /// <exception cref="ArgumentNullException">DisplayText parameter is null or empty.</exception>
        /// <exception cref="ArgumentNullException">LinkUrl parameter is null or empty.</exception>
        /// <param name="pageLinkKey">The unique identifier in the collection of links for the current
        /// page</param>
        /// <param name="displayText">The text that should be displayed to end users</param>
        /// <param name="linkUrl">The link URL to link to, including routed URLs</param>
        /// <param name="target">The optional target to include with the link</param>
        public WikiPageLink(int pageLinkKey, string displayText, string linkUrl, string target)
        {
            if (string.IsNullOrEmpty(displayText))
            {
                throw new ArgumentNullException("displayText");
            }
            if (string.IsNullOrEmpty(linkUrl))
            {
                throw new ArgumentNullException("linkUrl");
            }
            PageLinkKey = pageLinkKey;
            DisplayText = displayText;
            LinkUrl = linkUrl;
            Target = target != null ? target : string.Empty;
        }

        #endregion

        #region Public Methods

        public Control GetControl()
        {
            return new HyperLink()
            {
                Text = DisplayText,
                NavigateUrl = LinkUrl,
                Target = Target
            };
        }

        public string GetMarkup()
        {
            return string.Format("{{[link:{0}]}}", PageLinkKey);
        }

        public override bool Equals(object other)
        {
            if (other is IWikiPageComponent)
            {
                return Equals((IWikiPageComponent)other);
            }
            else
            {
                return false;
            }
        }

        public bool Equals(IWikiPageComponent other)
        {
            if (other is WikiPageLink)
            {
                return Equals((WikiPageLink)other);
            }
            else
            {
                return false;
            }
        }

        public bool Equals(WikiPageLink other)
        {
            if (other == null)
            {
                return false;
            }
            return PageLinkKey == other.PageLinkKey &&
                DisplayText.Equals(other.DisplayText) &&
                LinkUrl.Equals(other.LinkUrl) &&
                Target.Equals(other.Target);
        }

        #endregion
    }
}
